package com.infore.statisticsAndAlarm.export.service.impl;

import static org.hamcrest.CoreMatchers.nullValue;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.infore.statisticsAndAlarm.bo.DatasChartsBo;
import com.infore.statisticsAndAlarm.export.service.IExportService;
import com.infore.statisticsAndAlarm.export.util.ExportUtil;
import com.infore.statisticsAndAlarm.select.dto.QueryParamDto;
import com.infore.statisticsAndAlarm.select.util.PageObject;
import com.infore.statisticsAndAlarm.service.IDataReportsService;
import com.infore.statisticsAndAlarm.util.DateUtil;
import com.infore.statisticsAndAlarm.util.ObjectMapperUtil;
import com.infore.statisticsAndAlarm.util.ResultEntity;

@Service
public class ExportServiceImpl implements IExportService {

	@Autowired
	ObjectMapperUtil ObjectMapperUtil;
	@Autowired
	private IDataReportsService iDataReportsService;

	public byte[] exportExcelData(HttpServletRequest request,String flag) throws Exception {
		String params = request.getParameter("params").toString(); // 额外参数
		JSONObject paramsJson = JSONObject.parseObject(params);
		String headerProp = paramsJson.getString("headerProp"); // 数据映射属性名
		// 查询数据的参数
		DatasChartsBo bo = new DatasChartsBo();
		String stationCode = paramsJson.getString("stationCode");
		String factorCode = paramsJson.getString("factorCode");
		String startTime = paramsJson.getString("startTime");
		String endTime = paramsJson.getString("endTime");
		String startQuarter = paramsJson.getString("startQuarter");
		String endQuarter = paramsJson.getString("endQuarter");
		String compareType = paramsJson.getString("compareType");
		String compareStart = paramsJson.getString("compareStart");
		String compareEnd = paramsJson.getString("compareEnd");
		bo.setCompareEnd(compareEnd);
		bo.setCompareStart(compareStart);
		bo.setCompareType(compareType);
		bo.setEndQuarter(endQuarter);
		bo.setEndTime(endTime);
		bo.setFactorCode(factorCode);
		bo.setStartQuarter(startQuarter);
		bo.setStartTime(startTime);
		bo.setStationCode(stationCode);
		bo.setType(compareType);
		String pattern = paramsJson.getString("pattern"); // 时间类型
		PageObject pageObject = null;
		ResultEntity resultEntity = iDataReportsService.queryDatasStageReports(bo, pageObject);
		List<Map<String, Object>> list = ObjectMapperUtil.convertListMap(resultEntity);
		if (flag.equals("1")) {
			return ExportUtil.exportExcel(request, list, headerProp, pattern);
		} else if (flag.equals("2")) {
			return ExportUtil.exportPdf(request, list, headerProp, pattern);
		} else if (flag.equals("3")) {
			return ExportUtil.exportWord(request, list, headerProp, pattern);
		}else {
			return null;
		}
	}

	public byte[] waterQuality(HttpServletRequest request,String flag)  throws Exception {
		String params = request.getParameter("params").toString(); // 额外参数
		JSONObject paramsJson = JSONObject.parseObject(params);
		String headerProp = paramsJson.getString("headerProp"); // 数据映射属性名
		String pattern = paramsJson.getString("pattern"); // 时间类型
		// 查询参数
		String startTime = paramsJson.getString("startTime");
		String endTime = paramsJson.getString("endTime");
		String projectCode = paramsJson.getString("projectCode");
		String provinceId = paramsJson.getString("provinceId");
		String basinnId = paramsJson.getString("basinnId");
		String stationClassifyId = paramsJson.getString("stationClassifyId");
		String stationTypeId = paramsJson.getString("stationTypeId");
		String operCompanyId = paramsJson.getString("operCompanyId");
		String stationName = paramsJson.getString("stationName");
		String roleId = paramsJson.getString("roleId");
		PageObject pageObject = null;
		ResultEntity resultEntity = iDataReportsService.WaterQualityReports(startTime, endTime, projectCode, provinceId,
				basinnId, stationClassifyId, stationTypeId, operCompanyId, stationName,roleId, pageObject, request);
		List<Map<String, Object>> list = ObjectMapperUtil.convertListMap(resultEntity);
		if (flag.equals("1")) {
			return ExportUtil.exportExcel(request, list, headerProp, pattern);
		} else if (flag.equals("2")) {
			return ExportUtil.exportPdf(request, list, headerProp, pattern);
		} else if (flag.equals("3")) {
			return ExportUtil.exportWord(request, list, headerProp, pattern);
		}else {
			return null;
		}
	}

	public byte[] singleWaterQaulityReports(HttpServletRequest request,String flag) throws Exception  {
		String params = request.getParameter("params").toString(); // 额外参数
		JSONObject paramsJson = JSONObject.parseObject(params);
		String headerProp = paramsJson.getString("headerProp"); // 数据映射属性名
		// 查询参数
		String timeType = paramsJson.getString("timeType");
		String time = paramsJson.getString("time");
		String stationMn = paramsJson.getString("stationMn");
		String weekSatrt = paramsJson.getString("weekSatrt");
		String weekEnd = paramsJson.getString("weekEnd");
		String pattern = DateUtil.DATE_FULL_STR; // 时间类型--默认按小时
		if ("month".equals(timeType)){
			pattern = DateUtil.DATE_SMALL_STR;
		}else if ("year".equals(timeType)){
			pattern = DateUtil.DATE_SMALL_YM;
		}
		ResultEntity resultEntity = iDataReportsService.audioSingleReports(timeType, time, stationMn, weekSatrt, weekEnd);
		List<Map<String, Object>> list = ObjectMapperUtil.convertListMap(resultEntity);
		if (flag.equals("1")) {
			return ExportUtil.exportExcel(request, list, headerProp, pattern);
		} else if (flag.equals("2")) {
			return ExportUtil.exportPdf(request, list, headerProp, pattern);
		} else if (flag.equals("3")) {
			return ExportUtil.exportWord(request, list, headerProp, pattern);
		}else {
			return null;
		}
	}

	public byte[] moreWaterQaulityReports(HttpServletRequest request,String flag) throws Exception  {
		String params = request.getParameter("params").toString(); // 额外参数
		JSONObject paramsJson = JSONObject.parseObject(params);
		String headerProp = paramsJson.getString("headerProp"); // 数据映射属性名
		//String pattern = paramsJson.getString("pattern"); // 时间类型
		// 查询参数
		String timeType = paramsJson.getString("timeType");
		String time = paramsJson.getString("time");
		String stationMn = paramsJson.getString("stationMn");
		String weekSatrt = paramsJson.getString("weekSatrt");
		String weekEnd = paramsJson.getString("weekEnd");
		String pattern = DateUtil.DATE_FULL_STR; // 时间类型--默认按小时
		if ("month".equals(timeType)){
			pattern = DateUtil.DATE_SMALL_STR;
		}else if ("year".equals(timeType)){
			pattern = DateUtil.DATE_SMALL_YM;
		}
		ResultEntity resultEntity = iDataReportsService.audioMoreReports(timeType, time, stationMn, weekSatrt, weekEnd);
		List<Map<String, Object>> list = ObjectMapperUtil.convertListMap(resultEntity);
		if (flag.equals("1")) {
			return ExportUtil.exportExcel(request, list, headerProp, pattern);
		} else if (flag.equals("2")) {
			return ExportUtil.exportPdf(request, list, headerProp, pattern);
		} else if (flag.equals("3")) {
			return ExportUtil.exportWord(request, list, headerProp, pattern);
		}else {
			return null;
		}
	}

	public byte[] waterQaulityPublicReports(HttpServletRequest request,String flag)  throws Exception {
		String params = request.getParameter("params").toString(); // 额外参数
		JSONObject paramsJson = JSONObject.parseObject(params);
		String headerProp = paramsJson.getString("headerProp"); // 数据映射属性名
		String pattern = DateUtil.DATE_SMALL_YM; // 时间类型
		// 查询参数
		String timeType = paramsJson.getString("timeType");
		String time = paramsJson.getString("time");
		String queryCode = paramsJson.getString("queryCode");
		String queryType = paramsJson.getString("queryType");
		String userId = paramsJson.getString("userId");
		ResultEntity resultEntity = iDataReportsService.publicWaterQualityReports(timeType, time, queryCode, 
				queryType,userId,request);
		List<Map<String, Object>> list = ObjectMapperUtil.convertListMap(resultEntity);
		if (flag.equals("1")) {
			return ExportUtil.exportExcel(request, list, headerProp, pattern);
		} else if (flag.equals("2")) {
			return ExportUtil.exportPdf(request, list, headerProp, pattern);
		} else if (flag.equals("3")) {
			return ExportUtil.exportWord(request, list, headerProp, pattern);
		}else {
			return null;
		}
	}

}
